package com.alibaba.shopping_manager_api.controller;

import com.alibaba.shopping_common.pojo.Goods;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.GoodsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName GoodsController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/10 15:05
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @DubboReference
    private GoodsService goodsService;

    /**
     * 新增商品
     * @param goods
     * @return
     */
    @PostMapping("/add")
    public BaseResult<Goods> add(@RequestBody Goods goods){
        System.out.println("goods:"+goods);
        goodsService.add(goods);
        return BaseResult.OK(goods);
    }

    /**
     * 修改商品
     * @param goods
     * @return
     */
    @PutMapping("/update")
    public BaseResult<Goods> update(@RequestBody Goods goods){
        System.out.println("goods:"+goods);
        goodsService.update(goods);
        return BaseResult.OK(goods);
    }

    /**
     * 上架/下架商品
     * @param id
     * @param isMarketable
     * @return
     */
    @PutMapping("/putAway")
    public BaseResult putAway(Long id,Boolean isMarketable){
        System.out.println("id:"+id);
        System.out.println("isMarketable:"+isMarketable);
        goodsService.putAway(id,isMarketable);
        return BaseResult.OK();
    }

    /**
     * 根据ID查询Goods
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public BaseResult<Goods> findById(Long id){
        Goods goods = goodsService.findById(id);
        return BaseResult.OK(goods);
    }

    /**
     * 分页查询
     * @param goods 查询条件
     * @param page 页码
     * @param size 每页大小
     * @return
     */
    @GetMapping("/search")
    public BaseResult<Page<Goods>>search(Goods goods,int page,int size){
        Page<Goods> search = goodsService.search(goods, page, size);
        return BaseResult.OK(search);
    }

}




